package com.example.waterfall;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.waterfall.adapter.CommentLIstAdapter;
import com.example.waterfall.model.BitmapInfo;
import com.example.waterfall.model.CommentInfo;
import com.example.waterfall.model.Images;
import com.example.waterfall.util.ImageFetcher;
import com.example.waterfall.util.ImageUtils;
import com.example.waterfall.util.NetworkUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DetialActivity extends Activity implements OnClickListener, OnScrollListener {
	// ////////////////////////
	// handler，用以与其他非ui线程进行回调,数据传回
	// //////////////
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case LOAD_COMMENT_SUCCESS:
				//加载评论
				adapter.addItem(new CommentInfo());
				isLoading = false;
				adapter.notifyDataSetChanged();
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	};
	int i = 1;
	private static final String TAG = "DetialActivity";
	private ImageFetcher mImageFetcher;
	private com.example.waterfall.view.ScaleImageView2 iamgeView;
	private TextView comment;
	private Button good;
	private Button shared;
	private TextView tips;
	private EditText commentContent;
	private Button submit;
	private ListView commentList;
	private ViewGroup headView;
	 private int last_item_position;
	 private boolean isLoading = false;//是否加载过,控制加载次数   
	 private static final int LOAD_COMMENT_SUCCESS = 1<<1;

	private static final int LOAD_BITMAP_SUCCESS = 1;
	private CommentLIstAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pic_item_detials);
		init();

	}

	// ///////////////////////
	// 初始化
	// 1初始view
	// 初始监听
	// 获取数据，
	// 加载图片
	// 加载和图片相关的信息
	// 加载评论
	// ////////////////
	private void init() {
		initView();
		initListView();
		setOnListener();
		BitmapInfo info = getData();
		setBigPic();
		
	}

	/**
	 * 评论数据初始化
	 */
	private void initListView() {
		adapter = new CommentLIstAdapter();
		LayoutInflater layoutInflator = LayoutInflater.from(this);
		headView = (ViewGroup) layoutInflator.inflate(R.layout.pic_item_detials, null);
		
		//LISTview头啊
		iamgeView = (com.example.waterfall.view.ScaleImageView2) headView.findViewById(R.id.pic_detail_iv_image);
		comment = (TextView)  headView.findViewById(R.id.pic_detail_tv_comment);
		tips = (TextView)  headView.findViewById(R.id.pic_detail_tv_tips);
		good = (Button) headView.findViewById(R.id.pic_detail_bt_good);
		shared = (Button) headView.findViewById(R.id.pic_detail_bt_shared);
		commentContent = (EditText) headView.findViewById(R.id.waterfall_pic_comment_edit);
		submit = (Button) headView.findViewById(R.id.waterfall_pic_submit);
		
		
		commentList.addHeaderView(headView);
		TextView headText = new TextView(this);
		headText.setText("头2");
		commentList.addHeaderView(headText);
		commentList.setAdapter(adapter);
		commentList.setOnScrollListener(this);   
		//数据的杠取
		//数据添加
		adapter.addItemLast(getCommentInfo());
//		//数据的通知
		adapter.notifyDataSetChanged();
		//自定义的listview
		//下拉加载
		
		
	}

	// ////////////////////////
	// 初始View
	// ///////////////
	private void initView() {
		commentList = (ListView) findViewById(R.id.waterfall_pic_list_pic_cm);
	}

	// ///////////////////////
	// 初始监听
	// ////////////////
	private void setOnListener() {
		good.setOnClickListener(this);
		shared.setOnClickListener(this);
		submit.setOnClickListener(this);
	}

	/**
	 * 获取数据由传递获取。
	 * @return
	 */
	private BitmapInfo getData() {
		Toast.makeText(this, "获取数据:", 0).show();
		Intent intent = getIntent();
		BitmapInfo info = intent.getParcelableExtra(MainActivity.PAR_KEY);
		
		
		
		
		Log.d(TAG, info.icon);
		return info;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.pic_detail_bt_good:
			good();
			break;
		case R.id.pic_detail_bt_shared:
			shared(1);
			break;
		case R.id.waterfall_pic_submit:
			comment("评论");
			break;

		default:
			break;
		}
	}

	public void setBigPic() {
		// TODO 需要再下载大图片
		BitmapInfo info = getData();
		
		if(NetworkUtil.isAvailable(this)){
			//使用iamgefECTOR()
			mImageFetcher = new ImageFetcher(this, handler);
			mImageFetcher.setImageSize(info.imageWidth, info.imageHeight);
			iamgeView.setImageWidth(10*info.imageWidth);
			iamgeView.setImageHeight(10*info.imageHeight);
			mImageFetcher.setLoadingImage(R.drawable.empty_photo);//后台显示用
			mImageFetcher.loadImage(info.icon, iamgeView);
			
		}
		// 在下载大图之前的工作
		// 获取本地数据缩略图片
		// 做为默认显示作用
		// 开启异步张线程，再下载，new一个线程
	}

	
	
	// TODO
	// 设计一个效互类，负责交互处理
	// 设计一个个交互方法

	// ///////////////////////////
	// 交互方法集
	// ////////////////////

	// 顶
	private boolean good() {
		Toast.makeText(this, "顶了:", 0).show();
		//根据协议设计key value
		Map map = new HashMap();
		map.put("赞", 1);
		
	
		String url = "请求地址";
		boolean isDone = false;
		//本地做初始验算是不是已赞
		try {
			isDone = NetworkUtil.submitDataByDoGet(map, url);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		if(isDone){
			//服务器需要要验证是不是赞过。
			
			//本地显示加一
		}
		return true;
	}

	// 踩
	private boolean bad() {
		return true;
	}

	// 分享
	private boolean shared(int times) {
		// TODO 检查登录情况，调用共用方法
		Toast.makeText(this, "shared:", 0).show();
		//用户登录
		return true;
	}

	// 评论
	private boolean comment(String msg) {
		// TODO 检查登录情况，调用共用方法
		String str  = commentContent.getText().toString();
		comment.setText(str+":"+i++);
		CommentInfo info = new CommentInfo();
		info.textStr =str;
		//进行数据封装
		Map map = new HashMap();
		map.put("评论", str);
		
		String postUrl = "请求地址";
		boolean isDone = false;
		try {
			isDone = NetworkUtil.submintDataByHttpClientDoPost(map, postUrl);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(isDone){
			adapter.addItemTop(info);
			//数据的通知
			adapter.notifyDataSetChanged();
		}
		
		return true;
	}
	// ////////////////////
	// end交互方法集
	// /////////////////////////

	
	/**
	 * 获取评论等额外信息模拟数据
	 * 
	 * @return
	 */
	private List<CommentInfo> getCommentInfo() {
		List<CommentInfo> list = new ArrayList<CommentInfo>();
		for (int i = 0; i < Images.imageUrls.length; i++) {
			CommentInfo info = new CommentInfo();
			info.textStr = "评论" + i;
			list.add(info);
		}
		return list;
	}

	
	//////////////////////////////////
	//页面滚动监听听
	///////////////////////////////
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		last_item_position = firstVisibleItem + visibleItemCount - 1;  
		if(last_item_position==totalItemCount-1){
			System.out.println("开始加载.."); 
			
			if(!isLoading){
				//开启一个线程下载数据
				new Thread(){
					@Override
					public void run() {
						 isLoading = true;
						handler.sendEmptyMessage(LOAD_COMMENT_SUCCESS); 
						super.run();
					}
					
				}.start();
			}
			
		}
		
	}
	
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO 未处理
		
	}
}
